<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Config for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Config\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Config\Config;
use Zend\Config\Reader\Ini;
use Zend\Config\Reader\Xml;
use Zend\Config\Processor\Constant;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $configArray = array(
            'webhost' => 'www.example.com',
            'database' => array(
                'adapter' => 'pdo_mysql',
                'params' => array(
                    'host' => 'db.host.com',
                    'username' => 'dbuser',
                    'password' => 'secret',
                    'dbname' => 'mydatabase',
                ),
            ),
        );
        
        $configArr2 = array(
            'name' => 'lifuqiang'
        );
        
        $config = new Config($configArray);
        $config2 = new Config($configArr2);
        
        //第二个参数设置可以修改
//         $config = new Config($configArray, true);
//         $config->webhost = 'www.baidu.com';
        //合并config配置
//         $config->merge($config2);
        var_dump($config);
    }

    public function fooAction()
    {
        // This shows the :controller and :action parameters in default route
        // are working when you browse to /index/index/foo
        return array();
    }
    
    public function readAction()
    {
    	$iniReader = new Ini();
//     	echo dirname(__FILE__);
        echo "<hr /><br />Ini读取:";
           
    	$inidata = $iniReader->fromFile('path/to/config.ini');
    	var_dump($inidata);
    	
    	echo "<hr /><br />XML读取：";
    	$xmlReaer = new Xml();
    	$xmlData = $xmlReaer->fromFile('path/to/config2.xml');
    	var_dump($xmlData);
    }
    
    public function iniwriteAction()
    {
    	//首先创建config对象
    	$config = new Config(array(), true);
    	$config->production = array();
    	$config->production->webhost = 'www.baidu.com';
    	$config->production->database = array();
    	$config->production->database->params = array();
    	$config->production->database->params->host = 'localhost';
    	$config->production->database->params->username = "dbuser";
    	$config->production->database->params->pasword = "secret";
    	$config->production->database->params->dbname = "mydb";
    	
    	$writer = new \Zend\Config\Writer\Ini();
    	echo $writer->toString($config);
    	$writer->toFile('path/to/write/config.ini', $config);
    }
    
    public function processorAction()
    {
    	define('TEST_CONST', 'bar');
    	$config = new Config(array('foo' => 'TEST_CONST'), true);
    	$processor = new Constant();
    	echo $config->foo;
    	echo "<br />";
    	$processor->process($config);
    	echo $config->foo;
    }
}
